package cn.ehanghai.iot.back.controller;

import cn.ehanghai.iot.back.domain.ContentInfo;
import cn.ehanghai.iot.back.domain.UserInfo;
import cn.ehanghai.iot.back.domain.UserMessage;
import cn.ehanghai.iot.back.service.ContentInfoService;
import cn.ehanghai.iot.back.service.UserInfoService;
import cn.ehanghai.iot.back.service.UserMessageService;
import cn.ehanghai.iot.common.controller.BaseController;
import cn.ehanghai.iot.common.domain.PageSet;
import cn.ehanghai.iot.common.security.constants.Constants;
import cn.ehanghai.iot.common.security.constants.ReturnCode;
import cn.ehanghai.iot.vo.MessageWallVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户留言
 * @Author: xumf
 * @Date: 2019/6/26 18:44
 */
@RequestMapping("message")
@RestController
public class UserMessageController extends BaseController {

    @Autowired
    private UserMessageService userMessageService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private ContentInfoService contentInfoService;
    /**
     * 查询留言列表
     * @param pageNum
     * @param pageSize
     * @param phone
     * @param keyword
     * @param status
     * @param flag
     * @param startTime
     * @param endTime
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public ReturnCode list(@RequestBody Map<String, String> map){

        String pageNum=map.get("pageNum");
         String pageSize=map.get("pageSize");
         String phone=map.get("phone");
                String keyword=map.get("keyword");
                String status=map.get("status");
                String flag=map.get("flag");
                String startTime=map.get("startTime");
                String endTime=map.get("endTime");
        PageSet list=userMessageService.selectUserMessageListByPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize),phone,
                keyword,status,flag,startTime,endTime);
        return success(list);

    }

    /**
     * 设置状态值
     * 1上屏 0 取消上屏
     * @param status
     * @param ids 留言id
     */
    @RequestMapping(value = "setStatus",method = RequestMethod.POST)
    public ReturnCode setStatus(@RequestBody Map<String, Object> map){
        String status= (String) map.get("status");
        List<Integer> ids= (List<Integer>) map.get("ids");
//        String[] ids= (String[]) map.get("ids");
        for (Integer id:ids) {
            UserMessage userMessage=userMessageService.get(id.longValue());
            if(userMessage!=null){
                userMessage.setStatus(status);
                userMessageService.updateUserMessage(userMessage);
            }
        }
        return success();
    }

    /**
     * 设置标志
     * 1 加入篆刻 0 取消篆刻
     * @param flag
     * @param ids
     */
    @RequestMapping(value = "setFlag",method = RequestMethod.POST)
    public ReturnCode setFlag(@RequestBody Map<String, Object> map){
        String flag= (String) map.get("flag");
        List<Integer> ids= (List<Integer>) map.get("ids");
//        String ids[]= (String[]) map.get("ids");
        for (Integer id:ids) {
            UserMessage userMessage=userMessageService.get(id.longValue());
            if(userMessage!=null){
                userMessage.setCarveFlag(flag);
                userMessageService.updateUserMessage(userMessage);
            }
        }
        return success();
    }

    //-------公众号接口-------------

    /**
     * 写留言
     * @param openId
     * @param content
     */
    @RequestMapping(value = "write",method = RequestMethod.POST)
    public ReturnCode writeMessage(@RequestBody Map<String, String> map){
        String openId=map.get("openId");
        String content=map.get("content");
        UserInfo userInfo= userInfoService.getUserInfoByOpenId(openId);
        if(userInfo!=null) {
            UserMessage userMessage = userMessageService.selectUserMessageByOpenId(openId);
            if (userMessage == null) {
                userMessage = new UserMessage();
                userMessage.setCarveFlag("0");
                userMessage.setStatus("0");
                userMessage.setUserId(userInfo.getId());
                userMessage.setOpenId(openId);
                userMessage.setContent(content);
            } else {
                userMessage.setStatus("0");
                userMessage.setContent(content);
            }
            userMessageService.saveOrUpdate(userMessage);
            return success();
        }
        return errorException(Constants.ERROR_CODE_MEMBER_ERROR);
    }

    /**
     * 查看我的留言
     * @param openId
     */
    @RequestMapping(value = "viewMessage",method = RequestMethod.GET)
    public ReturnCode viewMyMessage(String openId){
        UserMessage userMessage = userMessageService.selectUserMessageByOpenId(openId);
        Map<String, Object> map = new HashMap<>();
        if(userMessage!=null) {
            map.put("status", userMessage.getStatus());
            map.put("content", userMessage.getContent());
        }else{
            map.put("status", "3");
            map.put("content", "");
        }
        return  success(map);
    }


    /**
     * 获取留言信箱墙
     */
    @RequestMapping("wall")
    public ReturnCode getMessageWall(){
        List<MessageWallVo> messageList= userMessageService.selectMessageWallList();
        return success(messageList);
    }

    /**
     * 获取文案列表
     * @return
     */
    @RequestMapping("content")
    public ReturnCode getContentList(){
        List<ContentInfo> list= contentInfoService.selectContentList();
        List<String> content=new ArrayList<>();
        for (ContentInfo c:list) {
            content.add(c.getContent());
        }
        return success(content);
    }


}
